/*******************************************************************************
 * File: status_led_task.c
 * Description:
 * Author: Mui Le
 * Date:
 ******************************************************************************/
#include "FreeRTOS.h"
#include "task.h"

#include "led.h"
#include "status_led_task.h"

/* macros */

// The stack for the LED toggle task.
#define LED_TASK_STACK_SIZE	    configMINIMAL_STACK_SIZE

// The amount of time to delay between toggles of the LED.
#define LED_TOGGLE_INTERVAL        500 /* ms */

#define LED_PIN            GPIO_Pin_12
#define LED_GPIO_PORT      GPIOD
#define LED_GPIO_CLK       RCC_AHB1Periph_GPIOD

/* local variables */

/*******************************************************************************
 * LOCAL Function:
 * Parameters:
 * Descryption:
 * Return:
 ******************************************************************************/
static xRET_CODE_TYPE
init_led_module(void){
    init_led(LED_GPIO_CLK, LED_GPIO_PORT, LED_PIN);
    return(xSUCCESS);
}

/*******************************************************************************
 * LOCAL Function:
 * Parameters:
 * Descryption:
 * Return:
 ******************************************************************************/
static xRET_CODE_TYPE
turn_on_led(void){
    on_led(LED_GPIO_PORT, LED_PIN);
    return(xSUCCESS);
}

/*******************************************************************************
 * LOCAL Function:
 * Parameters:
 * Descryption:
 * Return:
 ******************************************************************************/
static xRET_CODE_TYPE
turn_off_led(void){
    off_led(LED_GPIO_PORT, LED_PIN);
    return(xSUCCESS);
}

/*******************************************************************************
 * LOCAL Function: implement LED task
 * Parameters:
 *       param pvParameters
 * Descryption: This task simply toggles the user LED at a 1 Hz rate
 * Return: None
 ******************************************************************************/
static void
led_task_handler(void *pvParameters)
{
    portTickType xFlashRate, ulLastTime;

	xFlashRate = LED_TOGGLE_INTERVAL/portTICK_RATE_MS;

    //
    // Get the current tick count.
    //
    ulLastTime = xTaskGetTickCount();
    //
    // Loop forever.
    //
    while(1)
    {
        //
        // Turn on the user LED.
        //
        turn_on_led();
        //
        // Wait for the required amount of time.
        //
        vTaskDelayUntil( &ulLastTime, xFlashRate );
        //
        // Turn off the user LED.
        //
        turn_off_led();
        //
        // Wait for the required amount of time.
        //
        vTaskDelayUntil( &ulLastTime, xFlashRate );
    }
}

//*****************************************************************************
//API Function: Initializes the LED task.
//Parameters: None
//Descryption: Initialize hardware for task and create task
//Return: 0 if successful, else error code
//*****************************************************************************
xRET_CODE_TYPE
led_task_init(unsigned long xpriority)
{


    /*
     * Init HW
     */
    init_led_module();

    //
    // Create the LED task.
    //
    if(xTaskCreate(led_task_handler, (signed portCHAR *)"STATUS_LED",\
                   LED_TASK_STACK_SIZE,\
                   ( void * ) NULL, xpriority,\
                   NULL) != pdPASS){
        return(xERROR);
    }
    //
    // Success.
    //
    return(xSUCCESS);
}